<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>双十二狂欢节 - 淘乐乐商城</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            background: #f5f5f5;
            font-family: 'Microsoft YaHei', sans-serif;
        }
        
        .header {
            background: #ff4d4f;
            color: white;
            text-align: center;
            padding: 10px 0;
        }
        
        .main-banner {
            background: linear-gradient(to bottom, #ff4d4f, #ff6b6b);
            padding: 30px 0;
            text-align: center;
            color: white;
        }
        
        .festival-title {
            font-size: 36px;
            font-weight: bold;
            margin-bottom: 10px;
        }
        
        .festival-subtitle {
            font-size: 16px;
            margin-bottom: 20px;
        }
        
        .countdown-container {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin: 20px 0;
        }
        
        .countdown-box {
            background: rgba(255, 255, 255, 0.2);
            padding: 10px;
            border-radius: 4px;
            min-width: 80px;
        }
        
        .countdown-number {
            font-size: 32px;
            font-weight: bold;
        }
        
        .countdown-label {
            font-size: 12px;
            margin-top: 5px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .section-title {
            font-size: 24px;
            color: #333;
            margin: 20px 0;
            padding-left: 10px;
            border-left: 4px solid #ff4d4f;
        }
        
        .product-grid {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 20px;
        }
        
        .product-card {
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            transition: transform 0.3s;
        }
        
        .product-card:hover {
            transform: translateY(-5px);
        }
        
        .product-image {
            width: 100%;
            height: 200px;
            position: relative;
        }
        
        .product-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .discount-tag {
            position: absolute;
            top: 10px;
            right: 10px;
            background: #ff4d4f;
            color: white;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
        }
        
        .product-info {
            padding: 15px;
        }
        
        .product-name {
            font-size: 14px;
            margin-bottom: 10px;
            height: 40px;
            overflow: hidden;
        }
        
        .price {
            color: #ff4d4f;
            font-size: 20px;
            font-weight: bold;
        }
        
        .original-price {
            color: #999;
            font-size: 14px;
            text-decoration: line-through;
            margin-left: 5px;
        }
        
        .buy-btn {
            display: inline-block;
            width: 100%;
            background: #ff4d4f;
            color: white;
            text-align: center;
            padding: 8px 15px;
            border-radius: 4px;
            text-decoration: none;
            transition: background 0.3s;
            border: none;
            cursor: pointer;
        }
        
        .buy-btn:hover {
            background: #ff6b6b;
        }
        
        .coupon-section {
            margin: 30px 0;
            display: flex;
            gap: 20px;
            justify-content: center;
        }
        
        .coupon-card {
            background: linear-gradient(45deg, #ff4d4f, #ff6b6b);
            color: white;
            padding: 20px;
            border-radius: 8px;
            text-align: center;
            width: 200px;
        }
        
        .coupon-amount {
            font-size: 32px;
            font-weight: bold;
            margin: 10px 0;
        }
        
        .coupon-desc {
            font-size: 14px;
            margin-bottom: 10px;
        }
        
        .get-coupon-btn {
            background: white;
            color: #ff4d4f;
            border: none;
            padding: 5px 15px;
            border-radius: 15px;
            cursor: pointer;
        }
        
        .rules-section {
            background: white;
            padding: 20px;
            border-radius: 8px;
            margin-top: 30px;
        }
        
        .rules-title {
            font-size: 18px;
            color: #333;
            margin-bottom: 15px;
        }
        
        .rules-list {
            list-style: none;
            color: #666;
        }
        
        .rules-list li {
            margin-bottom: 10px;
            padding-left: 15px;
            position: relative;
        }
        
        .rules-list li:before {
            content: "•";
            color: #ff4d4f;
            position: absolute;
            left: 0;
        }
        
        .product-link {
            text-decoration: none;
            color: inherit;
            display: block;
        }
        
        .product-link:hover {
            text-decoration: none;
        }
        
        .button-group {
            display: flex;
            gap: 10px;
            padding: 10px 15px;
        }
        
        .cart-btn {
            display: inline-block;
            width: 100%;
            background: #fff1f0;
            color: #ff4d4f;
            text-align: center;
            padding: 8px 15px;
            border-radius: 4px;
            text-decoration: none;
            transition: all 0.3s;
            border: 1px solid #ff4d4f;
            cursor: pointer;
        }
        
        .cart-btn:hover {
            background: #ffd4d4;
        }
    </style>
</head>
<body>
    <%@ include file="header.jsp" %>
    
    <div class="main-banner">
        <div class="festival-title">双十二狂欢节</div>
        <div class="festival-subtitle">全场低至1折起 | 限时抢购 | 折上再减</div>
        <div class="festival-subtitle">2024年12月12日 00:00 抢购开始</div>
        <div class="countdown-container">
            <div class="countdown-box">
                <div class="countdown-number" id="days">00</div>
                <div class="countdown-label">天</div>
            </div>
            <div class="countdown-box">
                <div class="countdown-number" id="hours">00</div>
                <div class="countdown-label">时</div>
            </div>
            <div class="countdown-box">
                <div class="countdown-number" id="minutes">00</div>
                <div class="countdown-label">分钟</div>
            </div>
            <div class="countdown-box">
                <div class="countdown-number" id="seconds">00</div>
                <div class="countdown-label">秒</div>
            </div>
        </div>
    </div>

    <div class="container">
        <!-- 限时秒杀 -->
        <h2 class="section-title">限时秒杀</h2>
        <div class="product-grid">
            <c:forEach var="product" items="${flashSaleProducts}">
                <div class="product-card">
                    <a href="product?id=${product.productId}" class="product-link">
                        <div class="product-image">
                            <img src="images/${product.image}" alt="${product.name}">
                            <span class="discount-tag">5折</span>
                        </div>
                        <div class="product-info">
                            <h3 class="product-name">${product.name}</h3>
                            <div class="price">
                                ¥<fmt:formatNumber value="${product.price * product.discount}" pattern="#.00"/>
                                <span class="original-price">¥<fmt:formatNumber value="${product.price}" pattern="#.00"/></span>
                            </div>
                        </div>
                    </a>
                    <div class="button-group">
                        <button onclick="addToCart(${product.productId})" class="cart-btn">加入购物车</button>
                        <button onclick="buyNow(${product.productId}, '${product.name}', ${product.price}, ${product.discount})" class="buy-btn">立即抢购</button>
                    </div>
                </div>
            </c:forEach>
        </div>

        <!-- 新品首发 -->
        <h2 class="section-title">新品首发</h2>
        <div class="product-grid">
            <c:forEach var="product" items="${newProducts}">
                <div class="product-card">
                    <a href="product?id=${product.productId}" class="product-link">
                        <div class="product-image">
                            <img src="images/${product.image}" alt="${product.name}">
                            <span class="discount-tag">新品</span>
                        </div>
                        <div class="product-info">
                            <h3 class="product-name">${product.name}</h3>
                            <div class="price">
                                ¥<fmt:formatNumber value="${product.price * product.discount}" pattern="#.00"/>
                                <span class="original-price">¥<fmt:formatNumber value="${product.price}" pattern="#.00"/></span>
                            </div>
                        </div>
                    </a>
                    <div class="button-group">
                        <button onclick="addToCart(${product.productId})" class="cart-btn">加入购物车</button>
                        <button onclick="buyNow(${product.productId}, '${product.name}', ${product.price}, ${product.discount})" class="buy-btn">立即购买</button>
                    </div>
                </div>
            </c:forEach>
        </div>

        <!-- 热卖推荐 -->
        <h2 class="section-title">热卖推荐</h2>
        <div class="product-grid">
            <c:forEach var="product" items="${hotProducts}">
                <div class="product-card">
                    <a href="product?id=${product.productId}" class="product-link">
                        <div class="product-image">
                            <img src="images/${product.image}" alt="${product.name}">
                            <span class="discount-tag">热卖</span>
                        </div>
                        <div class="product-info">
                            <h3 class="product-name">${product.name}</h3>
                            <div class="price">
                                ¥<fmt:formatNumber value="${product.price * product.discount}" pattern="#.00"/>
                                <span class="original-price">¥<fmt:formatNumber value="${product.price}" pattern="#.00"/></span>
                            </div>
                        </div>
                    </a>
                    <div class="button-group">
                        <button onclick="addToCart(${product.productId})" class="cart-btn">加入购物车</button>
                        <button onclick="buyNow(${product.productId}, '${product.name}', ${product.price}, ${product.discount})" class="buy-btn">立即购买</button>
                    </div>
                </div>
            </c:forEach>
        </div>

        <!-- 优惠券 -->
        <div class="coupon-section">
            <div class="coupon-card">
                <div class="coupon-amount">¥50</div>
                <div class="coupon-desc">满500可用</div>
                <button class="get-coupon-btn" onclick="getCoupon(50)">立即领取</button>
            </div>
            <div class="coupon-card">
                <div class="coupon-amount">¥100</div>
                <div class="coupon-desc">满1000可用</div>
                <button class="get-coupon-btn" onclick="getCoupon(100)">立即领取</button>
            </div>
            <div class="coupon-card">
                <div class="coupon-amount">¥200</div>
                <div class="coupon-desc">满2000可用</div>
                <button class="get-coupon-btn" onclick="getCoupon(200)">立即领取</button>
            </div>
        </div>

        <!-- 活动规则 -->
        <div class="rules-section">
            <h3 class="rules-title">活动规则</h3>
            <ul class="rules-list">
                <li>活动时间：2024年12月12日 00:00-23:59</li>
                <li>所有商品活动限量，售完即止</li>
                <li>每个账号每个优惠券限领一次</li>
                <li>优惠券与其他优惠活动不可同时使用</li>
                <li>活动最终解释权归淘乐乐商城所有</li>
            </ul>
        </div>
    </div>

    <script>
        // 倒计时功能
        function updateCountdown() {
            const targetDate = new Date('2024-12-12T00:00:00').getTime();
            const now = new Date().getTime();
            const timeLeft = targetDate - now;

            const days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
            const hours = Math.floor((timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            const minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
            const seconds = Math.floor((timeLeft % (1000 * 60)) / 1000);

            document.getElementById('days').textContent = String(days).padStart(2, '0');
            document.getElementById('hours').textContent = String(hours).padStart(2, '0');
            document.getElementById('minutes').textContent = String(minutes).padStart(2, '0');
            document.getElementById('seconds').textContent = String(seconds).padStart(2, '0');
        }

        setInterval(updateCountdown, 1000);
        updateCountdown();

        // 领优惠券功能
        function getCoupon(amount) {
            // 检查用户是否登录
            <% if (session.getAttribute("userId") == null) { %>
                alert('请先登录后再领取优惠券！');
                window.location.href = 'login.jsp';
                return;
            <% } %>

            fetch('coupon/get', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    amount: amount
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('优惠券领取成功！');
                    const btn = event.target;
                    btn.disabled = true;
                    btn.textContent = '已领取';
                    btn.style.background = '#999';
                } else {
                    alert(data.message || '领取失败，请稍后重试');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('领取失败，请稍后重试');
            });
        }

        function buyNow(productId, productName, price, discount) {
            // 检查用户是否登录
            <% if (session.getAttribute("userId") == null) { %>
                alert('请先登录后再购买！');
                window.location.href = 'login.jsp';
                return;
            <% } %>
            
            // 创建订单对象
            const orderData = {
                productId: productId,
                productName: productName,
                quantity: 1,
                price: price,
                discount: discount,
                totalAmount: price * discount
            };
            
            // 将订单数据存储到sessionStorage
            sessionStorage.setItem('orderData', JSON.stringify(orderData));
            
            // 跳转到支付页面
            window.location.href = 'payment.jsp';
        }

        function addToCart(productId) {
            // 检查用户是否登录
            <% if (session.getAttribute("userId") == null) { %>
                alert('请先登录后再加入购物车！');
                window.location.href = 'login.jsp';
                return;
            <% } %>
            
            fetch('cart/add', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                body: new URLSearchParams({
                    productId: productId,
                    quantity: 1
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('已添加到购物车！');
                } else {
                    alert(data.message || '添加���败，请重试');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('添加失败，请重试');
            });
        }
    </script>

    <%@ include file="footer.jsp" %>
</body>
</html> 